﻿Imports System.Data.SqlServerCe

Public Class Citas
    Dim conexion As SqlCeConnection
    Dim fecha, hora As Date
    Dim empleado, observaciones As String
    Dim refAnimal As Integer
    Dim precio As Double




    Private Sub Citas_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Me.CenterToScreen()
    End Sub

    Private Sub Citas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'NuevoZooDBDataSet.Animales' table. You can move, or remove it, as needed.
Me.AnimalesTableAdapter.Fill(Me.NuevoZooDBDataSet.Animales)

        'TODO: This line of code loads data into the 'NuevoZooDBDataSet.Citas' table. You can move, or remove it, as needed.
        Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)
     
        conexion = New SqlCeConnection("Data Source=|DataDirectory|\NuevoZooDB.sdf")
    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub

    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick

        conexion.Open()

        Dim com As New SqlCeCommand("Select Nombre, Especie, FechaNacimiento from Animales Where id = @id", conexion)

        Try


            Dim dr As New DataGridViewRow
            dr = DataGridView2.SelectedRows(0)
            com.Parameters.AddWithValue("@id", dr.Cells(3).Value.ToString)

            Dim lector As SqlCeDataReader = com.ExecuteReader()
            lector.Read()

            LblNom.Text = lector(0).ToString
            LblEsp.Text = lector(1).ToString
            LblFecN.Text = lector(2).ToString.Replace("0:00:00", "")

            LblFec.Text = dr.Cells(1).Value.ToString.Replace("0:00:00", "")
            LblHora.Text = dr.Cells(2).Value.ToString.Remove(0, 10)
            LblEmp.Text = dr.Cells(4).Value.ToString

            conexion.Close()

        Catch ex As Exception
            MsgBox("Error cargando datos")
        End Try
    End Sub


 

    Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
        Dim fec As String = CStr(Me.MonthCalendar1.SelectionRange.Start).Replace("0:00:00", "")

        Me.CitasTableAdapter.FillByFecha(Me.NuevoZooDBDataSet.Citas, fec)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)
    End Sub



   

    Private Sub TabPage3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage3.Click

    End Sub

 

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)
    End Sub

    Private Sub BElCit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BElCit.Click
        Dim id As Integer
        Dim dr As New DataGridViewRow
        dr = DataGridView2.SelectedRows(0)
        id = dr.Cells(0).Value.ToString


        Dim resultado As MsgBoxResult = MsgBox("¿Desea eliminar el registro seleccionado?", MsgBoxStyle.YesNo, "Confirmacion de borrado")
        If resultado = MsgBoxResult.Yes Then


            Try
                Me.CitasTableAdapter.Delete(id)
                Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If
    End Sub

    

    Private Sub BAddCita_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BAddCita.Click
        Try
            Dim str As String
            str = DateTimePicker1.Text
            fecha = CDate(str)
            hora = DateTimePicker1.Text + " " + NumericUpDown1.Value.ToString + ":" + NumericUpDown2.Value.ToString
            refAnimal = CInt(TxtRefAn.Text)
            empleado = TxtEmp.Text
            precio = CDbl(Val(TxtPrec.Text))
            observaciones = TxtObs.Text

            Dim query As IEnumerable(Of Decimal) = _
                   From item As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                   Where item.Cells(0).Value IsNot DBNull.Value _
                   Select CDec(item.Cells(0).Value)

            ' Obtenemos el valor del id máximo
            '

            Dim id As Integer = query.ToList.Max() + 1

            If Login.validar.ValidarNombre(empleado) <> 3 Or empleado = "" Then
                MsgBox("El empleado no es válido", "Error")
            ElseIf precio = Nothing Then
                MsgBox("El precio  es incorrecto", , "Error")
            ElseIf id = 0 Then
                MsgBox("Debe seleccionar un registro de la tabla", , "Error")
            Else

                Try
                    Me.CitasTableAdapter.Insert(id, fecha, hora, refAnimal, empleado, precio, observaciones)
                    Me.CitasTableAdapter.Fill(Me.NuevoZooDBDataSet.Citas)
                    MsgBox("Cita añadida correctamente")
                    TabControl1.SelectedIndex = 0
                Catch ex As Exception
                    MsgBox("No se pudo guardar los datos")
                End Try
            End If
        Catch ex As Exception
            MsgBox("Los datos introducidos no son válidos")
        End Try
        
       


    End Sub

    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView2.SelectedRows(0)
        TxtRefAn.Text = dr.Cells(0).Value.ToString
    End Sub

    
    Private Sub MonthCalendar2_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar2.DateChanged
        Dim fec As String = CStr(Me.MonthCalendar2.SelectionRange.Start).Replace("0:00:00", "")

        Me.CitasTableAdapter.FillByFecha(Me.NuevoZooDBDataSet.Citas, fec)

    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        conexion.Open()

        Dim com As New SqlCeCommand("Select Nombre, Especie, FechaNacimiento from Animales Where id = @id", conexion)

        Try


            Dim dr As New DataGridViewRow
            dr = DataGridView2.SelectedRows(0)
            com.Parameters.AddWithValue("@id", dr.Cells(3).Value.ToString)

            Dim lector As SqlCeDataReader = com.ExecuteReader()
            lector.Read()

            TxtConNom.Text = lector(0).ToString
            TxtConEsp.Text = lector(1).ToString
            TxtConFeNa.Text = lector(2).ToString.Replace("0:00:00", "")

            TxtFec.Text = dr.Cells(1).Value.ToString.Replace("0:00:00", "")
            TxtHora.Text = dr.Cells(2).Value.ToString.Remove(0, 10)
            TxtConEmp.Text = dr.Cells(4).Value.ToString
            TxtConObs.Text = dr.Cells(6).Value.ToString

            conexion.Close()

        Catch ex As Exception
            MsgBox("Error cargando datos")
        End Try
    End Sub

    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub WebsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub WebsToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebsToolStripMenuItem.Click
        Webs.Show()
    End Sub

    Private Sub AyudaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AyudaToolStripMenuItem1.Click
        Dim rutaEjecutable As String = System.IO.Directory.GetCurrentDirectory().ToString
        System.Diagnostics.Process.Start(rutaEjecutable & "\Ayuda.chm")
    End Sub
End Class